package com.jgsu.sell.service.impl;

import com.jgsu.sell.dto.OrderDto;
import com.jgsu.sell.enums.ResultEnum;
import com.jgsu.sell.exception.SellException;
import com.jgsu.sell.service.BuyerService;
import com.jgsu.sell.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 描述:
 * 买家
 *
 * @author lqd12
 * @create 2018-08-20 23:46
 */
@Service
@Slf4j
public class BuyerServiceImpl implements BuyerService {

    @Autowired
    private OrderService orderService;
    @Override
    public OrderDto findOrderOne(String openid, String orderId) {
        OrderDto orderDto = checkOrderOwner(openid, orderId);
        return orderDto;
    }

    @Override
    public OrderDto cancelOrder(String openid, String orderId) {
        OrderDto orderDto = checkOrderOwner(openid, orderId);
        if(orderDto==null){
            log.error("【取消订单】，查询不到改订单，orderId={}",orderId);
            throw new SellException(ResultEnum.ORDER_NOT_EXIST);
        }
        return orderService.cancel(orderDto);
    }

    private OrderDto checkOrderOwner(String openid, String orderId){
        OrderDto orderDto = orderService.findOne(orderId);
        if(orderDto==null){
            return null;
        }
        if(!openid.equals(orderDto.getBuyerOpenid())){
            log.error("【查询订单】，订单的openid不一致，openid={},orderDto={}",openid,orderDto);
            throw new SellException(ResultEnum.ORDER_OWNER_ERROR);
        }
        return orderDto;
    }
}